package day06;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Demo08 {
    public static void main(String[] args) throws Exception{
        List<User> users = loadUsers("users.csv");
        users.forEach(System.out::println);
        //Java 系统默认编码:
        System.getProperties().list(System.out);
        //系统默认编码中 file.encoding 值设定了当前系统的默认文本文件编码
    }
    /**
     * 读取文本文件
     */
    public static List<User> loadUsers(String csv)
            throws IOException,
            UnsupportedEncodingException {
        List<User> list = new ArrayList<>();
        try (BufferedReader in = new BufferedReader(new InputStreamReader(
                new BufferedInputStream(
                        new FileInputStream(csv)),"GBK"))){
            //跳过第一行: line row
            String line = in.readLine();
            System.out.println(line);
            while ((line=in.readLine()) != null){
                //跳过空行
                if (line.trim().isEmpty()){
                    continue;
                }
                //跳过 # 开头的行
                if (line.startsWith("#")){
                    continue;
                }
                String[] arr = line.split(",");
                if (arr.length<4){
                    System.out.println("异常行:"+line);
                    continue;
                }
                int id = Integer.parseInt(arr[0]);
                String name = arr[1];
                double salary = Double.parseDouble(arr[2]);
                String address = arr[3];
                User user = new User(id, name, salary, address);
                list.add(user);
            }
        }
        return list;
    }
}
